#!/usr/bin/env python

import optparse
import sys

import charmicat

def main():
    '''
    ccat_print.py analysis_name [cutset]
    
    Print the names of cuts in `analysis.cutsets[cutset]'
    '''
    op = optparse.OptionParser(usage=main.__doc__)

    op.add_option("-s", "--start", type='int', dest='start', default=0, 
                  help='Start printing on the Nth cut in the list', metavar='N')
    op.add_option("-t", "--stop", type='int', dest='stop', default=None, 
                  help='Stop printing on the Nth cut in the list', metavar='N')
    op.add_option("-v", "--verbose", action='store_true', dest='verbose', default=False,
                  help='Print all cut information')
    
    opts, args = op.parse_args()

    if len(args) < 1:
        msg = "Please supply the name of an analysis package."
        raise charmicat.BadCommandLineError(msg, -1)        

    analysis_name = args.pop(0)

    analysis = charmicat.safe_import(analysis_name) 

    if len(args):
        cutset = args.pop(0)
    else:
        cutset = 'default'
           

    if cutset not in analysis.cutsets:
        msg = "Cut list `{0}' not found in `{1}.cutsets'; choices are"
        msg += "\t\n".join(sorted(analysis.cutsets.keys()))
        raise charmicat.BadCommandLineError(msg, -2)

    cut_list = [analysis.cuts[name] for name in analysis.cutsets[cutset]]

    for cut in cut_list[opts.start:opts.stop]:
        if opts.verbose:
            print cut.name, cut.pos_str
        else:
            print cut.name

    return 0


if __name__ == "__main__":
    try:
        sys.exit(main())
    except charmicat.BadCommandLineError, e:
        import os
        sys.stderr.write(str(e) + "\n")
        sys.stderr.write("See `{0} -h' for more.\n".format(os.path.basename(sys.argv[0])))
        sys.stderr.flush()
        sys.exit(e.retcode)


